home *** CD-ROM | disk | FTP | other *** search
-
- #include <stdio.h>
- struct employee {
- char fname[20], lname[20];
- long int hired;
- int grade;
- };
- main()
- {
- /* initialize employee data structure */
- static struct employee department[] = {
- {"John", "Doe",100185, 10 },
- {"Jim", "Doe",100188, 8 },
- {"Jane", "Doe",100183, 12 },
- {"", "",0, 0 }
- };
- struct employee input[10];
- int n,i;
- FILE *fp, *fopen();
-
- if( (fp=fopen("bfile","w+b")) == 0 ){
- printf("Could not open the file: %s\n","tempfile2");
- exit(1);
- }
-
- n=fwrite(department, sizeof(struct employee), 3, fp);
- printf("%d objects were written\n",n);
- rewind(fp); /* set file positioning pointer to start of file */
- /* read in "at most" 10 objects of type "struct employee" */
- n=fread(input, sizeof(struct employee), 10, fp);
- printf("%d objects were read from file\n",n);
- /* now print them out */
- for(i=0; i<n; i++){
- printf("Employee %s %s\n",input[i].fname,input[i].lname);
- printf("Hired: %ld, Grade: %d\n",input[i].hired,input[i].grade);
- }
- fclose(fp); /* remember to save file data */
-
- /* other interesting Standard C Library functions */
- if( (rename("bfile","bbfile")) != 0)
- printf("Attempt to rename file failed\n");
-
- if( (fp=fopen("bfile","rb")) == 0 )
- printf("Verified file was removed\n");
- else
- printf("Verified file was NOT removed\n");
-
- system("dir b*");
- }
-